package sii.challenge.dataset;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 
 * Singleton per la creazione di connessioni JDBC.
 * 
 * @author Marco Liceti
 *
 */
final class ConnectionFactory {
	
	private static ConnectionFactory instance;
	
	private ConnectionFactory() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			throw new Error("Driver JDBC non trovato.", e);
		}
	}
	
	static ConnectionFactory getInstance() {
		if (instance == null) {
			instance = new ConnectionFactory();
		}
		return instance;
	}
	
	Connection newConnection(String host, String port, String db, String user, String password) {
		Connection connection;
		String url = "jdbc:mysql://" + host + ":" + port + "/" + db;
		try {
			connection = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			throw new Error("Errore nella connesione al DB.", e);
		}
		return connection;
	}

}
